package com.zyg.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.zyg.shop.entity.SpecificationOptionEntity;
import com.zyg.shop.entity.TypeTemplateEntity;
import com.zyg.shop.entity.group.SpecificationOptionEntityVo;
import com.zyg.shop.service.SpecificationOptionService;
import com.zyg.shop.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.Query;

import com.zyg.shop.dao.SpecificationDao;
import com.zyg.shop.entity.SpecificationEntity;
import com.zyg.shop.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {
    @Autowired
    private TypeTemplateService typeTemplateService;
    @Autowired
    private SpecificationOptionService specificationOptionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Map> findSpecById(String typeId) {
        //通过模板id获取模板信息
        TypeTemplateEntity typeTemp = typeTemplateService.getById(typeId);
        //获取规格信息
        String specIds = typeTemp.getSpecIds();
        //将specIds转化为List集合方便进行遍历
        List<Map> maps = JSON.parseArray(specIds, Map.class);
        for (Map map : maps) {
            //获取规格id
            Integer id = (Integer)map.get("id");
            //根据规格id获取规格列表
            List<SpecificationOptionEntity> optionList = specificationOptionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));
            map.put("optionList",optionList);
        }

        return maps;
    }

}